package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.http.Http;
import com.zcx.helper.scale.ScaleScreenHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;
import com.zcx.helper.secret.SecretAESDESede;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import longcaisuyun.longcai.com.utils.MyPreferences;
import longcaisuyun.longcai.com.utils.MyViewUtil;
import wh.database.KJDB;

/**
 * Created by Administrator on 2016/2/18.
 */
public class MyApplication extends AppApplication {
    private Context context;
    public static MyPreferences myPreferences;
    public static ScaleScreenHelper ScaleScreenHelper;
    //加密
    public static SecretAESDESede secretAESDESede;
    public static MyViewUtil myviewutil;
    public static KJDB kjdb;
    public static AMapLocation loc;
    public List<Activity> list=new ArrayList<>();
    public static MyApplication instance;
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        myPreferences = new MyPreferences(this);
//        Helper.Initialize(this, "LongCaiSuYun");/**/
        Http.getInstance().setIsLog(true);
        myviewutil = new MyViewUtil();
        ScaleScreenHelper = ScaleScreenHelperFactory.create(this, 720);
        secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.AES_CBC_PKCS5PADDING);
//        secretAESDESede.encrypt();
//        secretAESDESede.decrypt();
        initImageLoader(getApplicationContext());
        kjdb = KJDB.create(getApplicationContext());
    }
    public static void initImageLoader(Context context) {
        //缓存文件的目录
        File cacheDir = StorageUtils.getOwnCacheDirectory(context, "168suyun/Cache");
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
                .threadPoolSize(5) //线程池内线程的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(20 * 1024 * 1024) // 内存缓存的最大值
                .diskCacheSize(100 * 1024 * 1024)  // SD卡缓存的最大值
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                        // 由原先的discCache -> diskCache
                .diskCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径
                .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
                .writeDebugLogs() // Remove for release app
                .build();
        //全局初始化此配置
        ImageLoader.getInstance().init(config);
//        JPushInterface.setDebugMode(true);
    }
    public static KJDB GetDbInstance() {
        if (kjdb != null) {
            return kjdb;
        }
        return null;
    }
    public static void init(Context context) {
//        JPushInterface.init(context);
    }
    public static void Stop(Context context) {
//        JPushInterface.stopPush(context);
    }
    public void addActivity(Activity activity){
        list.add(activity);
    }
    public void finishActivity(){
        for (int i = 0; i < list.size(); i++) {
            list.get(i).finish();
        }
    }
    public static MyApplication getInstance() {
        if (null == instance) {
            instance = new MyApplication();
        }
        return instance;
    }
}